import 'dart:io';

import 'package:common/base/base_state.dart';
import 'package:common/base/base_stateful_widget.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:resource/generated/l10n.dart';

class PathProviderPage extends BaseStatefulWidget {
  const PathProviderPage({super.key});

  @override
  State<PathProviderPage> createState() => _PathProviderState();
}

class _PathProviderState extends BaseState<PathProviderPage> {
  Future<Directory?>? _tempDirectory;
  Future<Directory?>? _appSupportDirectory;
  Future<Directory?>? _appLibraryDirectory;
  Future<Directory?>? _appDocumentsDirectory;
  Future<Directory?>? _appCacheDirectory;
  Future<Directory?>? _externalDocumentsDirectory;
  Future<List<Directory>?>? _externalStorageDirectories;
  Future<List<Directory>?>? _externalCacheDirectories;
  Future<Directory?>? _downloadsDirectory;

  void _requestTempDirectory() {
    setState(() {
      _tempDirectory = getTemporaryDirectory();
    });
  }

  Widget _buildDirectory(
      BuildContext context, AsyncSnapshot<Directory?> snapshot) {
    Text text = const Text('');
    if (snapshot.connectionState == ConnectionState.done) {
      if (snapshot.hasError) {
        text = Text('Error: ${snapshot.error}');
      } else if (snapshot.hasData) {
        text = Text('path: ${snapshot.data!.path}');
      } else {
        text = const Text('path unavailable');
      }
    }
    return Padding(padding: const EdgeInsets.all(16.0), child: text);
  }

  Widget _buildDirectories(
      BuildContext context, AsyncSnapshot<List<Directory>?> snapshot) {
    Text text = const Text('');
    if (snapshot.connectionState == ConnectionState.done) {
      if (snapshot.hasError) {
        text = Text('Error: ${snapshot.error}');
      } else if (snapshot.hasData) {
        final String combined =
        snapshot.data!.map((Directory d) => d.path).join(', ');
        text = Text('paths: $combined');
      } else {
        text = const Text('path unavailable');
      }
    }
    return Padding(padding: const EdgeInsets.all(16.0), child: text);
  }

  void _requestAppDocumentsDirectory() {
    setState(() {
      _appDocumentsDirectory = getApplicationDocumentsDirectory();
    });
  }

  void _requestAppSupportDirectory() {
    setState(() {
      _appSupportDirectory = getApplicationSupportDirectory();
    });
  }

  void _requestAppLibraryDirectory() {
    setState(() {
      _appLibraryDirectory = getLibraryDirectory();
    });
  }

  void _requestAppCacheDirectory() {
    setState(() {
      _appCacheDirectory = getApplicationCacheDirectory();
    });
  }

  void _requestExternalStorageDirectory() {
    setState(() {
      _externalDocumentsDirectory = getExternalStorageDirectory();
    });
  }

  void _requestExternalStorageDirectories(StorageDirectory type) {
    setState(() {
      _externalStorageDirectories = getExternalStorageDirectories(type: type);
    });
  }

  void _requestExternalCacheDirectories() {
    setState(() {
      _externalCacheDirectories = getExternalCacheDirectories();
    });
  }

  void _requestDownloadsDirectory() {
    setState(() {
      _downloadsDirectory = getDownloadsDirectory();
    });
  }

  @override
  void onStateInit() {
    rxTitle.value = S.current.get_native_path;
  }

  @override
  Widget onBodyCreate() {
    return Center(
      child: ListView(
        children: <Widget>[
          Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: ElevatedButton(
                  onPressed: _requestTempDirectory,
                  child: const Text(
                    'Get Temporary Directory',
                  ),
                ),
              ),
              FutureBuilder<Directory?>(
                future: _tempDirectory,
                builder: _buildDirectory,
              ),
            ],
          ),
          Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: ElevatedButton(
                  onPressed: _requestAppDocumentsDirectory,
                  child: const Text(
                    'Get Application Documents Directory',
                  ),
                ),
              ),
              FutureBuilder<Directory?>(
                future: _appDocumentsDirectory,
                builder: _buildDirectory,
              ),
            ],
          ),
          Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: ElevatedButton(
                  onPressed: _requestAppSupportDirectory,
                  child: const Text(
                    'Get Application Support Directory',
                  ),
                ),
              ),
              FutureBuilder<Directory?>(
                future: _appSupportDirectory,
                builder: _buildDirectory,
              ),
            ],
          ),
          Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: ElevatedButton(
                  onPressed:
                  Platform.isAndroid ? null : _requestAppLibraryDirectory,
                  child: Text(
                    Platform.isAndroid
                        ? 'Application Library Directory unavailable'
                        : 'Get Application Library Directory',
                  ),
                ),
              ),
              FutureBuilder<Directory?>(
                future: _appLibraryDirectory,
                builder: _buildDirectory,
              ),
            ],
          ),
          Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: ElevatedButton(
                  onPressed: _requestAppCacheDirectory,
                  child: const Text(
                    'Get Application Cache Directory',
                  ),
                ),
              ),
              FutureBuilder<Directory?>(
                future: _appCacheDirectory,
                builder: _buildDirectory,
              ),
            ],
          ),
          Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: ElevatedButton(
                  onPressed: !Platform.isAndroid
                      ? null
                      : _requestExternalStorageDirectory,
                  child: Text(
                    !Platform.isAndroid
                        ? 'External storage is unavailable'
                        : 'Get External Storage Directory',
                  ),
                ),
              ),
              FutureBuilder<Directory?>(
                future: _externalDocumentsDirectory,
                builder: _buildDirectory,
              ),
            ],
          ),
          Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: ElevatedButton(
                  onPressed: !Platform.isAndroid
                      ? null
                      : () {
                    _requestExternalStorageDirectories(
                      StorageDirectory.music,
                    );
                  },
                  child: Text(
                    !Platform.isAndroid
                        ? 'External directories are unavailable'
                        : 'Get External Storage Directories',
                  ),
                ),
              ),
              FutureBuilder<List<Directory>?>(
                future: _externalStorageDirectories,
                builder: _buildDirectories,
              ),
            ],
          ),
          Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: ElevatedButton(
                  onPressed: !Platform.isAndroid
                      ? null
                      : _requestExternalCacheDirectories,
                  child: Text(
                    !Platform.isAndroid
                        ? 'External directories are unavailable'
                        : 'Get External Cache Directories',
                  ),
                ),
              ),
              FutureBuilder<List<Directory>?>(
                future: _externalCacheDirectories,
                builder: _buildDirectories,
              ),
            ],
          ),
          Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: ElevatedButton(
                  onPressed: Platform.isAndroid || Platform.isIOS
                      ? null
                      : _requestDownloadsDirectory,
                  child: Text(
                    Platform.isAndroid || Platform.isIOS
                        ? 'Downloads directory is unavailable'
                        : 'Get Downloads Directory',
                  ),
                ),
              ),
              FutureBuilder<Directory?>(
                future: _downloadsDirectory,
                builder: _buildDirectory,
              ),
            ],
          ),
        ],
      ),
    );
  }
}